from selenium import webdriver

from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException,TimeoutException
from scrip import click
import time

from PIL import Image
import os


class Chaoxing():

    def __init__(self,password,username):
        chrome_opt=Options()
        chrome_opt.add_argument('--disable-infobars')
        chrome_opt.add_argument('--headless')
        chrome_opt.add_argument('--disable-gpu')
        chrome_opt.add_experimental_option('excludeSwitches', ['enable-automation'])
        self.browser=webdriver.Chrome(options=chrome_opt)
        self.username=username
        self.password=password

    def land(self):
        self.browser.get('http://jcut.fanya.chaoxing.com/portal')
        WebDriverWait(self.browser,30,0.2).until(lambda x: x.find_element_by_xpath('//input[@value= "登录"]')).click()
        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@id="unameId"]')).send_keys(self.username)
        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@id="passwordId"]')).send_keys(self.password)


        self.browser.get_screenshot_as_file('pei.png')
        img=self.browser.find_element_by_xpath('//img[@id="numVerCode"]')
        location=img.location
        size=img.size
        rangle=(326,334,428,376)
        Img=Image.open('pei.png')
        frame4=Img.crop(rangle)
        frame4.show()

        
        i=input('请输入验证码:')
        
        
        time.sleep(2)
        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@id="numcode"]')).send_keys(i)
        WebDriverWait(self.browser,30,0.2).until(lambda x: x.find_element_by_xpath('//input[@value= "登录"]')).click()

        print('成功登陆账号：{}'.format(self.username))


        path='pei.png'
        if os.path.exists(path):
            os.remove(path)


    def find_course(self):
        self.browser.switch_to.frame('frame_content')
        
        
        self.browser.find_element_by_xpath('//li[@style="position:relative"]')
        #course_name=self.browser.find_elements_by_xpath('//h3[@class="clearfix"]')
        course_name=self.browser.find_elements_by_xpath('//a[@title="WEB编程技术"]')
        WebDriverWait(self.browser,30,0.2).until(lambda x: x.find_element_by_xpath('//a[@title="WEB编程技术"]')).click()
        #click.click_couse(course_name,'创业基础')

        windows=self.browser.window_handles
        self.browser.switch_to.window(windows[-1])
        self.couse()

    def couse(self):

        class_num= -1

        while True:
            time.sleep(2)
            class_num=class_num+1
            class_name_list=self.browser.find_elements_by_xpath('//div[@class="leveltwo"]')
            if class_num == len(class_name_list):
                break
            else:
                print(class_num)
                class_name_num=self.browser.find_elements_by_xpath('//div[@class="leveltwo"]')[class_num].text
                print(class_name_num.split('\n'))
                if '1' == class_name_num.split('\n')[1]:
                    continue
                done=int(input('如果你已经看过一部分，请输入你已经看过的课程数(如没有请输入0) :'))
                class_name_tag= self.browser.find_elements_by_xpath('//span[@class="articlename"]')[class_num+done]
                class_name = class_name_tag.text
                class_name_tag.click()

                print('正在点击{}'.format(class_name))

                time.sleep(2)
                self.view(class_name=class_name)
                continue

    def view(self,class_name):
        #self.browser.find_element_by_xpath('//span[@title="视频"]').click()
        self.browser.switch_to.frame("iframe")
        time.sleep(5)
        iframe=self.browser.find_element_by_tag_name('iframe')
        self.browser.switch_to.frame(iframe)
        #self.browser.switch_to.frame(self.browser.find_element_by_xpath('//iframe[@class="ans-attach-online ans-insertvideo-online"]'))
        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//div[@id="video"]')).click()


        view_tag = self.browser.find_element_by_xpath('//div[@id="video"]')
        ActionChains(self.browser).move_to_element(view_tag).perform()
        while   True:
            time.sleep(2)

            if self.view_percentage() == '200':   
                self.browser.switch_to.default_content()
                self.browser.find_element_by_xpath('//a[contains(text(), "回到课程")]').click()

                break


    def view_percentage(self):
        """"
                  检查是否看完
        """
        # total_duration = self.browser.find_element_by_xpath('//span[@class="vjs-duration-display"]').text
        # current_duration = self.browser.find_element_by_xpath('//span[@class="vjs-current-time-display"]').text
        view_percentage_tag = self.browser.find_element_by_xpath('//div[@class="vjs-play-progress vjs-slider-bar"]')
        view_percentage = view_percentage_tag.get_attribute('style')
        print('当前进度'+view_percentage)
        self.is_exist_problem()
        """"
        检查是否看完
        """
        if '100%' in view_percentage :
            return '200'
        

    def is_exist_problem(self):
        try:
            problem_tag_style = WebDriverWait(self.browser, 30, 0.2).until(
                lambda x: x.find_element_by_xpath('//div[@id="ext-comp-1035"]')).get_attribute('style')

            if problem_tag_style == 'overflow: auto;':
                print('有题目')
                input_tag_list = self.browser.find_elements_by_xpath('//input')
                for input_tag in input_tag_list:
                    input_tag.click()
                    self.browser.find_element_by_xpath('//div[@class="ans-videoquiz-submit"]').click()
                    time.sleep(2)
                    if EC.alert_is_present()(self.browser):
                        self.browser.switch_to.alert.accept()
                    else:
                        break
            else:
                pass
        except UnexpectedAlertPresentException:
            print('alert出错')
            self.browser.switch_to.alert.accept()
        except TimeoutException:
            print('TimeoutException')
            pass
    



UserName=input('请输入你的学习通账号:')
PassWord=input('请输入密码:')
test=Chaoxing(PassWord,UserName)
test.land()
test.find_course()
